<template>
  <div class="guestbook-page">
    <!-- 背景图片 -->
    <div class="page_bg" :style="`background: url(${require('@/assets/images/indb.jpg')}) center top no-repeat;`"></div>
    
    <!-- 主要内容 -->
    <div class="container">
      <div class="row">
        <!-- 右侧主内容区域 -->
        <div class="col-xs-12 col-sm-8 col-md-9" style="float:right">
          <div class="list_box">
            <h2 class="left_h2">在线留言</h2>
            <div class="feedback">
              <form @submit.prevent="submitForm" class="form-horizontal">
                <div class="form-group">
                  <label for="title" class="col-sm-3 control-label">留言标题：</label>
                  <div class="col-sm-6">
                    <input 
                      type="text" 
                      v-model="form.title" 
                      class="form-control" 
                      id="title" 
                      placeholder="输入留言标题"
                    >
                  </div>
                </div>
                
                <div class="form-group">
                  <label for="name" class="col-sm-3 control-label">您的姓名：</label>
                  <div class="col-sm-6">
                    <input 
                      type="text" 
                      v-model="form.name" 
                      class="form-control" 
                      id="name" 
                      placeholder="您的姓名"
                    >
                  </div>
                </div>
                
                <div class="form-group">
                  <label for="tel" class="col-sm-3 control-label">联系手机：</label>
                  <div class="col-sm-6">
                    <input 
                      type="text" 
                      v-model="form.tel" 
                      class="form-control" 
                      id="tel" 
                      placeholder="输入手机号"
                    >
                  </div>
                </div>
                
                <div class="form-group">
                  <label for="email" class="col-sm-3 control-label">电子邮箱：</label>
                  <div class="col-sm-6">
                    <input 
                      type="email" 
                      v-model="form.email" 
                      class="form-control" 
                      id="email" 
                      placeholder="输入电子邮箱"
                    >
                  </div>
                </div>
                
                <div class="form-group">
                  <label for="address" class="col-sm-3 control-label">联系地址：</label>
                  <div class="col-sm-6">
                    <input 
                      type="text" 
                      v-model="form.address" 
                      class="form-control" 
                      id="address" 
                      placeholder="输入联系地址"
                    >
                  </div>
                </div>
                
                <div class="form-group">
                  <label for="content" class="col-sm-3 control-label">留言内容：</label>
                  <div class="col-sm-6">
                    <textarea 
                      v-model="form.content" 
                      class="form-control" 
                      id="content"
                      rows="5" 
                      placeholder="请输入您的留言内容"
                    ></textarea>
                  </div>
                </div>
                
                <div class="form-group">
                  <div class="col-sm-offset-3 col-sm-10">
                    <button 
                      type="submit" 
                      class="btn btn-primary" 
                      :disabled="isSubmitting"
                    >
                      {{ isSubmitting ? '提交中...' : '提交表单' }}
                    </button>
                    &nbsp;
                    <button 
                      type="button" 
                      @click="resetForm" 
                      class="btn btn-default grey-btn"
                    >
                      重新填写
                    </button>
                  </div>
                </div>
              </form>
            </div>
          </div>
        </div>
        
        <!-- 左侧边栏 -->
        <div class="col-xs-12 col-sm-4 col-md-3">
          <!-- 导航菜单 -->
          <div class="left_nav" id="categories">
            <h2 class="left_h2">在线留言</h2>
            <ul class="left_nav_ul" id="firstpane">
              <!-- 这里可以添加留言相关的导航项 -->
            </ul>
          </div>
          
          <!-- 搜索框 -->
          <form @submit.prevent="performSearch">
            <div class="input-group search_group">
              <input 
                type="text" 
                v-model="searchKeyword"
                class="form-control input-sm" 
                placeholder="信息查询"
              >
              <span class="input-group-btn">
                <button @click="performSearch" class="btn btn-sm mysearch_btn" type="button">
                  搜 索
                </button>
              </span>
            </div>
          </form>
          
          <!-- 养宠知识 -->
          <div class="left_news">
            <h2 class="left_h2">养宠知识</h2>
            <ul>
              <li>
                <router-link to="/pet-column/article/7" title="猫为什么会炸毛">
                  猫为什么会炸毛
                </router-link>
                <span class="news_time">04/22</span>
              </li>
            </ul>
          </div>
          
          <!-- 联系我们 -->
          <div class="left_contact">
            <h2 class="left_h2" style="margin-bottom:20px;">联系我们</h2>
            <p>联系人：贫僧</p>
            <p>手机：暂无</p>
            <p>电话：暂无</p>
            <p>邮箱：暂无</p>
            <p>地址：暂无</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'GuestbookPage',
  data() {
    return {
      searchKeyword: '',
      isSubmitting: false,
      form: {
        title: '',
        name: '',
        tel: '',
        email: '',
        address: '',
        content: ''
      }
    }
  },
  methods: {
    performSearch() {
      if (this.searchKeyword.trim()) {
        this.$router.push({ 
          path: '/pet-column', 
          query: { search: this.searchKeyword } 
        })
      }
    },
    
    validateForm() {
      if (!this.form.name.trim()) {
        alert('请输入您的姓名！')
        return false
      }
      
      if (!this.form.tel.trim()) {
        alert('请输入你的手机！')
        return false
      }
      
      // 手机号格式验证
      const telRegex = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(17[0-9]{1})|(18[0-9]{1})|(19[0-9]{1}))+\d{8})$/
      if (!telRegex.test(this.form.tel)) {
        alert('手机号码格式不正确！')
        return false
      }
      
      if (!this.form.content.trim()) {
        alert('请输入留言内容！')
        return false
      }
      
      return true
    },
    
    async submitForm() {
      if (!this.validateForm()) {
        return
      }
      
      this.isSubmitting = true
      
      try {
        // 这里模拟提交过程
        await new Promise(resolve => setTimeout(resolve, 1500))
        
        alert('留言提交成功！我们会尽快回复您。')
        this.resetForm()
      } catch (error) {
        alert('提交失败，请稍后重试！')
        console.error('Form submission error:', error)
      } finally {
        this.isSubmitting = false
      }
    },
    
    resetForm() {
      this.form = {
        title: '',
        name: '',
        tel: '',
        email: '',
        address: '',
        content: ''
      }
    }
  }
}
</script>

<style scoped>
.guestbook-page {
  padding-top: 0; /* 使用全局body padding-top */
}

.page_bg {
  height: 200px;
  background-size: cover !important;
  background-position: center !important;
  margin-bottom: 30px;
}

.list_box {
  background: white;
  padding: 30px;
  border-radius: 5px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  margin-bottom: 20px;
}

.left_h2 {
  color: #04A07B;
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 15px;
  padding-bottom: 10px;
  border-bottom: 2px solid #04A07B;
}

.feedback {
  margin-top: 20px;
}

.form-horizontal .form-group {
  margin-bottom: 20px;
}

.form-horizontal .control-label {
  text-align: right;
  font-weight: 500;
  color: #333;
  padding-top: 7px;
}

.form-control {
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 8px 12px;
  font-size: 14px;
  transition: border-color 0.3s ease;
}

.form-control:focus {
  border-color: #04A07B;
  box-shadow: 0 0 0 2px rgba(4, 160, 123, 0.2);
  outline: none;
}

.btn-primary {
  background-color: #04A07B;
  border-color: #04A07B;
  color: white;
  padding: 8px 20px;
  font-size: 14px;
  border-radius: 4px;
  transition: all 0.3s ease;
}

.btn-primary:hover:not(:disabled) {
  background-color: #038066;
  border-color: #038066;
}

.btn-primary:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-default {
  background-color: #f8f9fa;
  border-color: #ddd;
  color: #666;
  padding: 8px 20px;
  font-size: 14px;
  border-radius: 4px;
  transition: all 0.3s ease;
}

.btn-default:hover {
  background-color: #e2e6ea;
  border-color: #dae0e5;
}

textarea.form-control {
  resize: vertical;
  min-height: 100px;
}

/* 侧边栏样式 */
.left_nav, .left_news, .left_contact {
  background: white;
  padding: 20px;
  border-radius: 5px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  margin-bottom: 20px;
}

.left_nav_ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

.left_nav_ul li {
  border-bottom: 1px solid #eee;
}

.left_nav_ul li:last-child {
  border-bottom: none;
}

.biglink {
  display: block;
  padding: 12px 0;
  color: #333;
  text-decoration: none;
  font-weight: 500;
}

.biglink:hover {
  color: #04A07B;
}

.biglink.curr {
  color: #04A07B;
  font-weight: bold;
}

.search_group {
  margin-bottom: 20px;
}

.mysearch_btn {
  background: #04A07B;
  color: white;
  border: none;
}

.mysearch_btn:hover {
  background: #038066;
}

.left_news ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

.left_news li {
  padding: 8px 0;
  border-bottom: 1px solid #eee;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.left_news li:last-child {
  border-bottom: none;
}

.left_news a {
  color: #333;
  text-decoration: none;
  flex: 1;
  margin-right: 10px;
  font-size: 14px;
}

.left_news a:hover {
  color: #04A07B;
}

.news_time {
  color: #999;
  font-size: 12px;
}

.left_contact p {
  margin-bottom: 8px;
  color: #666;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .col-xs-12.col-sm-8.col-md-9 {
    float: none !important;
    order: 2;
  }
  
  .col-xs-12.col-sm-4.col-md-3 {
    order: 1;
    margin-bottom: 20px;
  }
  
  .guestbook-page {
    padding-top: 0; /* 使用全局body padding-top */
  }
  
  .list_box {
    padding: 20px;
  }
  
  .form-horizontal .control-label {
    text-align: left;
    margin-bottom: 5px;
  }
  
  .form-horizontal .col-sm-3,
  .form-horizontal .col-sm-6,
  .form-horizontal .col-sm-10,
  .form-horizontal .col-sm-offset-3 {
    width: 100%;
    padding-left: 15px;
    padding-right: 15px;
  }
  
  .form-horizontal .col-sm-offset-3 {
    margin-left: 0;
  }
}

/* 表单验证状态 */
.form-control.error {
  border-color: #dc3545;
  box-shadow: 0 0 0 2px rgba(220, 53, 69, 0.2);
}

.form-control.success {
  border-color: #28a745;
  box-shadow: 0 0 0 2px rgba(40, 167, 69, 0.2);
}

/* 提交状态 */
.form-submitting {
  position: relative;
  opacity: 0.7;
  pointer-events: none;
}

.form-submitting::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255, 255, 255, 0.8);
  z-index: 10;
  border-radius: 5px;
}
</style>
